﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    httpservice.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2024-12-23  16:27
*/

#include "httpservice.h"

#include <QCoreApplication>
#include <QSettings>

QMutex HttpService::mutex;
HttpService* HttpService::instance = nullptr;


HttpService& HttpService::getInstance()
{
    if (instance == nullptr) {
        QMutexLocker locker(&mutex);
        if (instance == nullptr) {
            instance = new HttpService();
        }
    }
    return *instance;
}

void HttpService::deleteInstance()
{
    if (instance != nullptr) {
        delete instance;
        instance = nullptr;
    }
}

void HttpService::installController(BaseController *controller, QString path)
{
    controller->registerRoutes(httpServer);
}

HttpService::HttpService(QObject *parent) : QObject(parent)
{
    qDebug() << __FUNCTION__;
    //注册类型
    qRegisterMetaType<int8_t>("int8_t");
    qRegisterMetaType<uint8_t>("uint8_t");
    qRegisterMetaType<int16_t>("int16_t");
    qRegisterMetaType<uint16_t>("uint16_t");
    qRegisterMetaType<int32_t>("int32_t");
    qRegisterMetaType<uint32_t>("uint32_t");
    qRegisterMetaType<int64_t>("int64_t");
    qRegisterMetaType<uint64_t>("uint64_t");
//    httpServer = new QHttpServer();
}

HttpService::~HttpService()
{
    stop();
}

void HttpService::start()
{
    const auto port = httpServer.listen(QHostAddress::Any,8080);
    if (!port) {
        qDebug() << QCoreApplication::translate(
                "QHttpServerExample", "Server failed to listen on a port.");
    }else{
        qDebug() << QCoreApplication::translate(
                "QHttpServerExample", "Running on http://127.0.0.1:%1/ (Press CTRL+C to quit)").arg(port);
    }


}

void HttpService::stop()
{
//    delete httpServer;
}
